package com.itheima.common;

import com.itheima.exception.CustomException;
import com.itheima.exception.SetmealException;
import com.itheima.pojo.Employee;
import com.itheima.pojo.R;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExeptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R exception(SQLIntegrityConstraintViolationException s){
        R<Employee> employeeR = new R<>();
        employeeR.setCode(0);
        employeeR.setMsg("用户名重复");
        return employeeR;
    }
    @ExceptionHandler(CustomException.class)
    public R exception(CustomException c){
       return R.error(c.getMessage());
    }
    @ExceptionHandler(IOException.class)
    public R exception(IOException i){
        return R.error("发生未知错误!");
    }

    @ExceptionHandler(SetmealException.class)
    public R exception(SetmealException s){
        return R.error("发生未知错误!");
    }

}
